package com.superzqb.asus.superzqbFragment.superzqbShopMain.Shop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.superzqb.asus.Api.CampusShopApi;
import com.superzqb.asus.Api.ShopApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop.Adapter.CommentAdapter;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop.Bean.CommentBean;
import com.superzqb.asus.superzqbFragment.superzqbShopMain.Bean.GoodsDetailBean;
import com.superzqb.asus.superzqbUtil.GenCommentsAdapter;
import com.superzqb.asus.superzqbUtil.GeneralFun;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.superzqb.asus.Api.ApiParam.imgUrl;
import static com.superzqb.asus.Api.ApiParam.maxItem;
import static com.superzqb.asus.superzqbUtil.GeneralFun.randomArray;

public class GoodsContent extends Activity implements View.OnClickListener{
    private TextView title;
    private Context context;
    private ImageView back;
    private Spinner model;
    private ArrayAdapter<String> modelDataAdapter;
    private String[] modelData;
    private ConvenientBanner convenient;//顶部广告栏控件
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private PullRefreshLayout layout;
    private int lastItem;
    private int listSize;
    private int pagenum=1;
    private List<CommentBean> listAll=new ArrayList<>();
    private CommentAdapter adapter;
    private String gid;
    private boolean isButtom=false;
    private List<String> images =new ArrayList<>();
    private ListView commentList;
    private GoodsDetailBean map;
    private Intent intent;
    //private  GenCommentsAdapter adapter;
    //动态数组保存要显示的数据，数据存放在数组中，<键，值>对
    public static ArrayList<Map<String, Object>> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_content);
        initView();
        //获取轮播图
        for(int i=0;i<images.size();i++) {
            images.set(i,imgUrl + images.get(i));
        }
        new GeneralFun().setCarousel1(context,convenient,images,i.width,i.height);
        adapter = new CommentAdapter(context, listAll);
        commentList.setAdapter(adapter);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        getLv();
                    }
                }, 2000);
            }
        });
        commentList.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    if (lastItem == listSize) {
                        if(!isButtom) {
                            pagenum++;
                         /*   Toast.makeText(context,
                                    "加载第" + pagenum + "页",
                                    Toast.LENGTH_SHORT).show();*/

                            getLv();
                        }else
                            Toast.makeText(context,
                                    "没有更多了",
                                    Toast.LENGTH_SHORT).show();

                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = commentList.getLastVisiblePosition();

            }
        });
    }

    private void initView() {
        title=(TextView)findViewById(R.id.title);
        intent=getIntent();
        map=(GoodsDetailBean)intent.getSerializableExtra("map");
        images.addAll(map.getImages());
        title.setText(map.getName().length()>20?"商品详情":map.getName());
        context=GoodsContent.this;
        convenient = (ConvenientBanner)findViewById(R.id.convenientBanner);
        layout= (PullRefreshLayout)findViewById(R.id.swipeRefreshLayout);
        commentList=(ListView)findViewById(R.id.commentList);
        li=(LinearLayout)findViewById(R.id.top);
        back=(ImageView)findViewById(R.id.back);
        model=(Spinner)findViewById(R.id.model);
        modelData= getResources().getStringArray(R.array.iphone);
        modelDataAdapter=new ArrayAdapter<>(this,R.layout.simple_spinner_item, modelData);
        modelDataAdapter.setDropDownViewResource(R.layout.drop_down_item);
        model .setAdapter(modelDataAdapter);
        model.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               //  Toast.makeText(context, "您点击的是"+modelData[position], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        Display d=GoodsContent.this.getWindowManager().getDefaultDisplay();
        i=li.getLayoutParams();
        i.height=d.getHeight()/3;
        i.width=d.getWidth();

        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            default:break;
        }
    }

    public void getLv() {
        Retrofit retrofit=new GeneralFun().gsonCreate();
        ShopApi api=retrofit.create(ShopApi.class);
        Call<List<CommentBean>> call=api.getCommentList(pagenum);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                if(response.isSuccessful()){
                    List<CommentBean> tmp=response.body();

                    if(tmp.size()!=0) {
                        listAll.addAll(getRandomCommentBean(tmp));
                        listSize = listAll.size() - 1;
                        adapter.setList(listAll);
                        adapter.notifyDataSetChanged();
                        if(tmp.size()!=maxItem)
                            isButtom=true;
                    }else{
                        isButtom=true;
                    }

                }
            }

            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable throwable) {

                //  Toast.makeText(context,throwable.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });

    }
    @Override
    public void onResume() {
        super.onResume();
        mClear();
        getLv();
    }
    private void mClear() {
        listAll.clear();
        listSize=0;
        pagenum=1;
        isButtom=false;
    }

    public List<CommentBean>  getRandomCommentBean(List<CommentBean> list) {
        List<CommentBean> listBean=new ArrayList<>();
        int a[]=randomArray(0,list.size()-1,5);
        for(int i=0;i<a.length;i++){
            listBean.add(list.get(a[i]));
        }
        return listBean;
    }

}
